<template>
  <div class='page-information'>
    <div class="information-left">
      <div class="title">
        <img src="/img/pc/news.png" alt=""><span>资讯</span>
      </div>
      <div class="news-box" v-for="(item,index) in news_list" :key="index">
        <div class="main-img">
          <img :src="item.mainDiagram" alt="" @click="linkTo('/information-detail?id=' + item.id)">
        </div>
        <div class="content">
          <div class="news-top">
            <div class="news-title" @click="linkTo('/information-detail?id=' + item.id)">
              {{item.title}}
            </div>
            <div class="news-date">
              <span>{{item.author}}</span>
              <span>{{trimDate(item.publishTime)}}</span>
            </div>
          </div>
          <div class="news-info">
            <span>{{getSimpleText(item.introduce)}}</span>
          </div>
        </div>
      </div>
      <div class="footer">
        <Page :total="total" :page-size="pageSize" :current="page" @on-change="handleChangePage" />
      </div>

    </div>
    <div class="information-right">
      <div class="hot-title">
        <div class="hot-left">热门课程</div>
        <div class="hot-right" @click="linkTo('/classify')">查看跟多
          <Icon type="ios-arrow-forward" />
        </div>
      </div>
      <div class="hot-course" v-if="classes.length > 0">
        <div class="course-box" v-for="(item, index) in classes" :key="index" @click="linkTo('/coursedetail?id=' + item.id)">
          <div class="main-img">
            <img :src="item.iconPath" alt="">
          </div>
          <div class="course-title">
            {{item.courseName}}
          </div>
          <div class="teacher">
            <div class="teacher-name">
              <span>老师 | {{item.acName}}</span>
            </div>

            <div class="teacher-author">
              <img :src="item.avatar" alt="">
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions, mapMutations } from "vuex";
import { getNewsList } from "~/api/news";
import { getCourse } from "~/api/course";
export default {
  data() {
    return {
      news_list: [],
      pageSize: 5,
      page: 1,
      total: 0,
      classes: [], //推荐课程
    };
  },
  created() {
    this.getNews();
    this.initNavList();
    this.getRecCourse();
  },
  mounted() {},
  methods: {
    ...mapMutations(["SET_IS_LOADING"]),
    ...mapActions(["initNavList"]),
    //   html剔除富文本标签，留下第一段纯文本
    getSimpleText(html) {
      let re1 = new RegExp("<.+?>", "g"); //匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
      let msg = html.replace(re1, ""); //执行替换成空字符
      let msg2 = msg.split("=");
      let msg3 = msg2.filter((i, x) => i != "")[0];
      return msg3;
    },
    //格式化时间
    trimDate(date) {
      if (date) {
        let _date = new Date(date);
        return `${_date.getFullYear()}-${
          _date.getMonth() + 1
        }-${_date.getDate()}`;
      } else {
        return "";
      }
    },
    // 路由跳转
    linkTo(path) {
      this.$router.push({
        path,
      });
    },
    //分页处理
    handleChangePage(n) {
      this.page = n;
      this.getNews();
    },
    //获取资讯
    async getNews() {
      this.SET_IS_LOADING(true);
      let params = {
        sort: "id desc",
        offset: this.pageSize * (this.page - 1),
        limit: this.pageSize,
        isOpen:1,
      };
      let result = await getNewsList(params);
      if (result && result.data && result.data.code == 200) {
        this.news_list = result.data.data.rows;
        this.total = result.data.data.total;
        // console.log("news_list", result.data.data.rows);
      } else {
        this.$Message.error(this.$t("locale.pc.index.t9"));
      }
      this.SET_IS_LOADING(false);
    },
    async getRecCourse() {
      this.SET_IS_LOADING(true);
      let params = {
        offset: 0,
        limit: 4,
        sort: "sortIndex desc",
        isRec: 1,
      };
      let result = await getCourse(params);
      if (result && result.data && result.data.code == 200) {
        this.SET_IS_LOADING(false);
        this.classes = result.data.data.rows;
      } else {
        this.SET_IS_LOADING(false);
        this.$Message.error(
          this.$t("locale.pc.mytest.testTrain.t25") + result &&
            result.data &&
            result.data.msg
        );
      }
    },
  },
};
</script>
<style lang='scss' scoped>
.page-information {
  width: 1200px;
  margin: 0 auto;
  padding: 60px 0;
  display: flex;

  .information-left {
    width: 80%;
    box-sizing: border-box;
    padding-right: 60px;
    padding-left: 20px;

    .title {
      font-size: 24px;
      font-weight: bold;
      color: #666666;
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      img {
        display: block;
        background-color: var(--theme-color);
      }
    }
    .news-box {
      width: 100%;
      height: 160px;
      // background-color: red;
      // margin-bottom: 20px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #e6ebf0;
      // padding: 10px 0;
      .main-img {
        width: 160px;
        height: 106.66px;
        background-color: var(--theme-color);
        overflow: hidden;
        border-radius: 4px;
        img {
          width: 100%;
          height: 100%;
          display: block;
          cursor: pointer;
        }
      }
      .content {
        width: 700px;
        height: 108px;
        .news-top {
          display: flex;
          justify-content: space-between;
          .news-title {
            width: 70%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            font-size: 16px;
            font-weight: bold;
            line-height: 24px;
            color: #4a4b4d;
            cursor: pointer;
          }
          .news-title:hover{
            color: var(--theme-color);
          }
          .news-date {
            display: flex;
            justify-content: space-between;
            width: 28%;
            font-size: 14px;
            font-weight: bold;
            line-height: 24px;
          }
        }
        .news-info {
          margin-top: 20px;
          font-size: 14px;
          font-weight: bold;
          line-height: 20px;
          display: -webkit-box;
          -webkit-line-clamp: 3;
          overflow: hidden;
          text-overflow: ellipsis;
          -webkit-box-orient: vertical;
          height: 60px;
        }
      }
    }
    .footer {
      text-align: center;
      margin-top: 20px;
    }
  }
  .information-right {
    width: 20%;
    background-color: #ffffff;
    .hot-title {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 14px 20px;
      box-sizing: border-box;
      // border-bottom: 1px solid #edf2fa;
      .hot-left {
        font-size: 16px;
        font-weight: bold;
        color: var(--theme-color);
      }
      .hot-right {
        font-size: 14px;
        font-weight: bold;
        color: #aaadb3;
        cursor: pointer;
        // line-height: 14px;
        display: flex;
        align-items: center;
      }
      .hot-right:hover {
        color: var(--theme-color);
        // text-decoration: underline;
      }
    }
    .hot-course {
      width: 100%;
      .course-box {
        width: 100%;
        padding: 20px;
        box-sizing: border-box;
        border-top: 1px solid #edf2fa;
        cursor: pointer;
        .main-img {
          width: 100%;
          height: 120px;
          img {
            width: 100%;
            height: 100%;
            display: block;
          }
        }
        .course-title {
          width: 100%;
          max-height: 44px;
          margin-top: 10px;
          font-size: 14px;
          font-weight: bold;
          line-height: 22px;
          color: #494a4d;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          overflow: hidden;
          text-overflow: ellipsis;
          -webkit-box-orient: vertical;
        }
        .teacher {
          display: flex;
          align-items: center;
          justify-content: space-between;
          // margin-top: 10px;
          .teacher-name {
            font-size: 12px;
            font-weight: bold;
            color: #757980;
          }

          .teacher-author {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            overflow: hidden;
            background-color: var(--theme-color);
          }
        }
      }
    }
  }
}
</style>